package com.example.sicnuaccountingapp.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AnticipateOvershootInterpolator
import androidx.core.view.ViewCompat
import androidx.core.view.ViewPropertyAnimatorListener
import androidx.recyclerview.widget.RecyclerView

object AnimationUtils {
    private const val DEFAULT_DURATION = 300L

    fun fadeIn(view: View, duration: Long = DEFAULT_DURATION) {
        view.alpha = 0f
        view.visibility = View.VISIBLE
        ViewCompat.animate(view)
            .alpha(1f)
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .start()
    }

    fun fadeOut(view: View, duration: Long = DEFAULT_DURATION) {
        ViewCompat.animate(view)
            .alpha(0f)
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .setListener(object : ViewPropertyAnimatorListener {
                override fun onAnimationStart(view: View) {}
                override fun onAnimationEnd(view: View) {
                    view.visibility = View.GONE
                }
                override fun onAnimationCancel(view: View) {}
            })
            .start()
    }

    fun slideIn(view: View, duration: Long = DEFAULT_DURATION) {
        view.translationY = view.height.toFloat()
        view.visibility = View.VISIBLE
        ViewCompat.animate(view)
            .translationY(0f)
            .setDuration(duration)
            .setInterpolator(AnticipateOvershootInterpolator())
            .start()
    }

    fun slideOut(view: View, duration: Long = DEFAULT_DURATION) {
        ViewCompat.animate(view)
            .translationY(view.height.toFloat())
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .setListener(object : ViewPropertyAnimatorListener {
                override fun onAnimationStart(view: View) {}
                override fun onAnimationEnd(view: View) {
                    view.visibility = View.GONE
                }
                override fun onAnimationCancel(view: View) {}
            })
            .start()
    }

    fun scaleIn(view: View, duration: Long = DEFAULT_DURATION) {
        view.scaleX = 0f
        view.scaleY = 0f
        view.visibility = View.VISIBLE
        ViewCompat.animate(view)
            .scaleX(1f)
            .scaleY(1f)
            .setDuration(duration)
            .setInterpolator(AnticipateOvershootInterpolator())
            .start()
    }

    fun scaleOut(view: View, duration: Long = DEFAULT_DURATION) {
        ViewCompat.animate(view)
            .scaleX(0f)
            .scaleY(0f)
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .setListener(object : ViewPropertyAnimatorListener {
                override fun onAnimationStart(view: View) {}
                override fun onAnimationEnd(view: View) {
                    view.visibility = View.GONE
                }
                override fun onAnimationCancel(view: View) {}
            })
            .start()
    }

    fun animateRecyclerView(recyclerView: RecyclerView) {
        recyclerView.alpha = 0f
        recyclerView.translationY = 100f
        recyclerView.animate()
            .alpha(1f)
            .translationY(0f)
            .setDuration(DEFAULT_DURATION)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .start()
    }

    fun animateHeightChange(view: View, targetHeight: Int) {
        val startHeight = view.height
        val valueAnimator = ValueAnimator.ofInt(startHeight, targetHeight).apply {
            duration = DEFAULT_DURATION
            interpolator = AccelerateDecelerateInterpolator()
            addUpdateListener { animator ->
                val params = view.layoutParams
                params.height = animator.animatedValue as Int
                view.layoutParams = params
            }
        }
        valueAnimator.start()
    }
} 